package dk.modified.reshary;

import java.io.File;
import java.util.ArrayList;

import org.apache.http.Header;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import dk.modified.reshary.GlobalState.TrackerName;

public class ViewOwnerShare extends FragmentActivity {

	private static final int REPORT_IMAGE_ACTIVITY_REQUEST_CODE = 13214;

	private CustomViewPager viewPager;
	private ArrayList<Owners> arraylist;
	private Owners currentOwner;
	private RelativeLayout container_bottom_bar;
	private ImageButton cmd_report;
	private ImageButton cmd_browse;
	private ImageButton cmd_share;
	private ImageButton cmd_follow;
	private ImageButton cmd_comment;
	private TextView txt_image_title;
	private TextView txt_owner_name;
	private TextView txt_share_number;
	private int currentIndex = 0;
	private int initialIndex;
	private Boolean isFavorite;
 	private ImageAdapter adapter; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_owner_share);
		Tracker t = ((GlobalState) getApplication())
				.getTracker(TrackerName.APP_TRACKER);
		t.setScreenName("Reshary Show Shares Viewpager");
		t.send(new HitBuilders.AppViewBuilder().build());
		getActionBar().setDisplayHomeAsUpEnabled(true);
		String title = getIntent().getExtras().getString("title");
		Util.setActionBar(this, title);
		instantiateViews();

		Bundle extras = getIntent().getExtras();
		arraylist = extras.getParcelableArrayList("arraylist");
		initialIndex = extras.getInt("position");
		viewPager = (CustomViewPager) findViewById(R.id.view_pager);
		

		adapter = new ImageAdapter(this, arraylist, getActionBar()
				.getHeight(), getSupportFragmentManager());
		
		
		if (getIntent().getExtras().containsKey("favorite")) {
			isFavorite = true;
		} else
			isFavorite = false;

		adapter.setOnBottomBarHiddenListener(new onSetBottomBarHidden() {

			@Override
			public void show() {
				container_bottom_bar.setVisibility(View.VISIBLE);
			}

			@Override
			public void hide() {
				container_bottom_bar.setVisibility(View.GONE);
			}
		});

		viewPager.setAdapter(adapter);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int index) {
				// TODO Auto-generated method stub
				currentIndex = index;
				currentOwner = arraylist.get(index);
				txt_image_title.setText(currentOwner.getTitle());
				txt_owner_name.setText(currentOwner.getOwner());
				txt_share_number.setText(currentOwner.getShares());
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

		viewPager.setCurrentItem(initialIndex);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == REPORT_IMAGE_ACTIVITY_REQUEST_CODE) {
			if (resultCode == RESULT_OK) {
				String issue = data.getExtras().getString("issue");
				String text = data.getExtras().getString("reason");
				String mediaid = String.valueOf(currentOwner.getId());
				ReportMedia(mediaid, issue, text);
			}
		}
	}

	private void instantiateViews() {
		container_bottom_bar = (RelativeLayout) findViewById(R.id.container_bottom_bar);
		cmd_report = (ImageButton) findViewById(R.id.cmd_report);
		cmd_browse = (ImageButton) findViewById(R.id.cmd_browse);
		cmd_browse.setVisibility(View.GONE);
		cmd_share = (ImageButton) findViewById(R.id.cmd_share);
		cmd_follow = (ImageButton) findViewById(R.id.cmd_follow);
		cmd_follow.setVisibility(View.GONE);
		cmd_comment = (ImageButton) findViewById(R.id.cmd_comment);
		txt_image_title = (TextView) findViewById(R.id.txt_image_title);
		txt_owner_name = (TextView) findViewById(R.id.txt_owner_name);
		txt_share_number = (TextView) findViewById(R.id.txt_share_number);
		setListeners();
	}

	private void setListeners() {
		cmd_report.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getApplicationContext(),
						ReportActivity.class);
				startActivityForResult(i, REPORT_IMAGE_ACTIVITY_REQUEST_CODE);
			}
		});

		cmd_share.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int index = viewPager.getCurrentItem();
				share(arraylist.get(index).getSharingurl());
			}
		});

		cmd_comment.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int index = viewPager.getCurrentItem();
				Intent startCommentActivity = new Intent(
						getApplicationContext(), CommentActivity.class);

				startCommentActivity.putExtra("mediaid",
						Integer.parseInt(arraylist.get(index).getId()));
				startActivity(startCommentActivity);
				overridePendingTransition(R.anim.fadeout, R.anim.fadein);
			}
		});
	}

	private void share(String url) {
		Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
		sharingIntent.setType("text/plain");
		sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
				"Reshary image");
		String shareMessage = getResources().getString(
				R.string.share_media_text)
				+ " " + url;
		sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareMessage);

		startActivity(Intent.createChooser(sharingIntent, "Share"));
	}

	private void ReportMedia(String mediaid, String issue, String reporttext) {

		SharedPreferences sharedpreferences = getSharedPreferences(
				"dk.modified.reshary", Context.MODE_PRIVATE);
		String userid = sharedpreferences.getString("userid", "null");
		String securitytoken = sharedpreferences.getString("securitytoken",
				"null");

		RequestParams params = new RequestParams();
		params.put("userid", userid);
		params.put("securitytoken", securitytoken);
		params.put("reason", reporttext);
		params.put("issue", issue);
		params.put("mediaid", String.valueOf(mediaid));

		ResharyRestClient.post("reportit/", params,
				new AsyncHttpResponseHandler() {

					@Override
					public void onFailure(int arg0, Header[] arg1, byte[] arg2,
							Throwable arg3) {
						// TODO
						Log.d("TEST", arg3.getMessage().toString());
						super.onFailure(arg0, arg1, arg2, arg3);
					}

					@Override
					public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
						super.onSuccess(arg0, arg1, arg2);
						String toastText = getResources().getString(
								R.string.report_success);
						Toast.makeText(getApplicationContext(), toastText,
								Toast.LENGTH_LONG).show();
					}

				}, this);

	}

	@Override
	public void onBackPressed() {
		if (currentIndex != 0 && currentIndex != initialIndex) {
			Bundle b = new Bundle();
			b.putInt("position", currentIndex);

			Intent returnIntent = new Intent();
			returnIntent.putExtras(b);
			setResult(RESULT_OK, returnIntent);
			super.onBackPressed();
			overridePendingTransition(R.anim.fadeout, R.anim.fadein);
		}
		super.onBackPressed();
	}



}
